<template>
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="handleCloseDialog"
    @open="handleOpen"
  >
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartDetail, reqUpdateDepartments } from '@/api/departments'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    deptsList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 这个函数的目的是 去找 同级部门下(父id相同) 是否有重复的部门名称
    const checkNameRepeat = (rule, value, callback) => {
      let arr = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        console.log(this.deptsList)
        arr = this.deptsList.filter(item => this.nodeData.pid === item.pid)
        console.log(arr, value)
      } else {
        arr = this.deptsList.filter(item => item.pid === this.nodeData.id)
      }
      const flag = arr.some(item => value === item.name)
      flag ? callback(new Error('已经存在的部门了')) : callback()
    }
    // 校验部门编码是否重复
    const checkCodeRepeat = (rule, value, callback) => {
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }
      // 所有部门, 编码都唯一, 不管层级
      const flag = this.deptsList.some((item) => item.name === value)
      flag ? callback(new Error(`已经有编码${value}的部门了`)) : callback()
    }
    return {
      peoples: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: ['blur', 'change']
          },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: ['blur', 'change']
          },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: ['blur', 'change']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    handleCloseDialog() {
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 管理者
        introduce: '' // 部门介绍
      }
      this.$emit('update:showDialog', false)
      this.$refs.deptForm.resetFields()
    },
    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      this.peoples = res.data
      console.log(this.peoples)
    },
    handleOpen() {
      this.getEmployeeSimple()
    },
    clickSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (valid) {
          if (this.form.id) {
          // 编辑
            await reqUpdateDepartments(this.form)
            this.$message.success('更新部门成功')
          } else {
            // 调用新增接口 添加父部门的id
            await reqAddDepartments({ ...this.form, pid: this.nodeData.id })
            // 成功提示
            this.$message.success('新增部门成功')
          }
        }
        // 通知到父组件, 添加操作
        this.$emit('add-depts')
        // 关闭弹窗
        this.handleCloseDialog()
      })
    },
    async getDepartmentDetail() {
      const res = await reqGetDepartDetail(this.nodeData.id)
      this.form = res.data
    }
    // 子传父, 通知到父组件, 将布尔值改成false
    // 重置数据
    // this.form = {
    //   name: '', // 部门名称
    //   code: '', // 部门编码
    //   manager: '', // 管理者
    //   introduce: '' // 部门介绍
    // }
    // this.$emit('update:showDialog', false)
    // this.$refs.deptForm.resetFields()
  }
}
</script>
